package com.johnson.news.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.johnson.news.R;
import com.johnson.news.databinding.ActivityModifyPwdBinding;
import com.johnson.news.model.bean.DataUserInfo;
import com.johnson.news.model.remote.RemoteDataSource;
import com.johnson.news.model.util.NewsPreference;
import com.johnson.news.util.Utils;
import com.youmbe.library.BaseBindingActivity;
import com.youmbe.library.util.MyToast;

import io.reactivex.functions.Consumer;

public class ModifyPwdActivity extends BaseBindingActivity<ActivityModifyPwdBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void init() {
        dataBinding.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.getId() == R.id.enter_btn) {
                modify();
            }
        }
    };

    boolean checkPhone() {
        if(TextUtils.isEmpty(dataBinding.phoneEdit.getText())) {
            dataBinding.phoneEdit.setError(getString(R.string.login_null_phone));
            return false;
        }
        if(!Utils.verifyPhone(dataBinding.phoneEdit.getText().toString())) {
            dataBinding.phoneEdit.setError(getString(R.string.login_invalid_phone));
            return false;
        }

        return true;
    }

    void modify() {

        if(!checkPhone())
            return;

        if(TextUtils.isEmpty(dataBinding.newPwdEdit.getText())) {
            dataBinding.newPwdEdit.setError(getString(R.string.login_null_password));
            return;
        }
        if(TextUtils.isEmpty(dataBinding.againPwdEdit.getText())) {
            dataBinding.againPwdEdit.setError(getString(R.string.login_null_password));
            return;
        }

        if(!dataBinding.againPwdEdit.getText().toString().equals(dataBinding.newPwdEdit.getText().toString())) {
            dataBinding.againPwdEdit.setError(getString(R.string.register_diff_password));
            return;
        }

        if(dataBinding.newPwdEdit.getText().length() < 6) {
            dataBinding.newPwdEdit.setError(getString(R.string.register_invalid_password));
            return;
        }

        String token = new NewsPreference(activityInstance).getToken();
        RemoteDataSource.getRemoteDataSource()
                .modifyPwd(dataBinding.phoneEdit.getText().toString(), token, dataBinding.newPwdEdit.getText().toString())
                .subscribe(new Consumer<DataUserInfo>() {
                    @Override
                    public void accept(DataUserInfo dataUserInfo) throws Exception {
                        MyToast.showToast("密码修改成功。", activityInstance);
                        finish();
                    }
                });

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_modify_pwd;
    }

}
